package com.lplb.common.coreconfig.config;


import com.lplb.common.core.result.RType;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 自定义异常类，通过此类可返回各种自定义异常信息，由GlobalExceptionHandler 处理返回
 * <p>
 * 使用：throw new ErrorException("1000000","自定义异常测试");
 * 返回：{"code": "1000000","msg": "自定义异常测试"}
 *
 * @author ws
 * @mail 1720696548@qq.com
 * @date 2020/2/9 0009 12:44
 * @return
 */
@Component
@NoArgsConstructor
@Data
public class ErrorException extends RuntimeException {

    private static final long serialVersionUID = -3247538758440382976L;
    private Integer code;
    private String msg;
    private String errorMsg;

    //直接传递
    public ErrorException(Integer code, String msg,String errorMsg) {
        this.code = code;
        this.msg = msg;
        this.errorMsg = errorMsg;
    }
    //直接传递
    public ErrorException(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.errorMsg = null;
    }

    //枚举传递（建议先定义枚举）
    public ErrorException(RType rType) {
        this.code = rType.getCode();
        this.msg = rType.getMsg();
        this.errorMsg = null;
    }

    //枚举传递（建议先定义枚举）
    public ErrorException(RType rType,String errorMsg) {
        this.code = rType.getCode();
        this.msg = rType.getMsg();
        this.errorMsg = errorMsg;
    }
}
